package com.zj.leetcode.easy;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @program algorithm
 * @description:
 * @author: zhangbo
 * @create: 2022/01/08 23:51
 */
public class ValidAnagram {
    public static void main(String[] args) {
        String s = "anagram", t = "nagaram";
//        String s = "aa", t = "bb";
//        String s = "rat", t = "car";
        Solution solution = new ValidAnagram().new Solution();
        System.out.println(solution.isAnagram(s, t));

        System.out.println(solution.isAnagram01(s, t));

    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public boolean isAnagram(String s, String t) {

            Map<Character, Integer> map = new HashMap<>();

            if(s.length() != t.length()){
                return false;
            }

            for (int i = 0; i < s.length(); i++) {
                map.put(s.charAt(i), map.getOrDefault(s.charAt(i), 0) + 1);
            }
            for (int i = 0; i <t.length(); i++) {
                Integer orDefault = map.getOrDefault(t.charAt(i), 0);
                if(orDefault == 0){
                    return false;
                }
                map.put(t.charAt(i),  orDefault - 1);
            }
            return true;

        }

        public boolean isAnagram01(String s, String t) {
            if(s.length() != t.length()){
                return false;
            }
            return Arrays.equals(s.chars().sorted().toArray(), t.chars().sorted().toArray());
        }


    }
//leetcode submit region end(Prohibit modification and deletion)

}
